window.onload=function(){
    var obj={
        /*所有的灯 */
        lights:[
            {//绿灯
                light:document.getElementById('green'),
                timeout:10,
                style:['green','gray','gray'],
                next:1
            },
            {//黄灯
                light:document.getElementById('yellow'),
                timeout:5,
                style:['gray','yellow','gray'],
                next:2
            },
            {//红灯
                light:document.getElementById('red'),
                timeout:10,
                style:['gray','gray','red'],//当前灯凉的时候的亮灯样式
                next:0
            }
        ],
        currentLight:1,//当前亮灯的编号
        changeToNext:function(){//当 当前灯倒计时 为0 时，调用；以切换当前灯编号
            this.currentLight = this.lights[this.currentLight].next;
            this.timeout = this.lights[this.currentLight].timeout;//改变当前倒计时数
            let light = this.lights[this.currentLight];
            /*this.lights[0].light.classList.add(light.style[0]);
            this.lights[1].light.classList.add(light.style[1]);
            this.lights[2].light.classList.add(light.style[2]);
          */
            this.lights[0].light.style.backgroundColor=light.style[0];
            this.lights[1].light.style.backgroundColor=light.style[1];
            this.lights[2].light.style.backgroundColor=light.style[2];
        },
        start:function(){ //启动红绿灯
            this.currentLight = 0;
            this.changeToNext();
            setInterval(this.countDown,1000);
        },
        timeout:0,//当前倒计时数
        countDown:function(){//倒计时方法
            obj.timeout --;
            if(obj.timeout == 0){
                obj.changeToNext();
            }
            document.getElementsByClassName('count')[0].innerHTML=obj.timeout+"";
        }
    }
    obj.start();
    


}